/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef MODULAR_WIFI
#define MODULAR_WIFI

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "stm32f4xx.h"
#include "serial_task.h"

#define TCP_SERVICE_IP    "\"192.168.1.123\""   // 丽红
#define WIFI_SSID  "\"YYYYYY\""
#define TCP_COM    5000
#define WIFI_PASSWORD "\"66666666\""
#define WIFI_DATA_LEN 128

typedef struct {
    char buffer[WIFI_DATA_LEN];
    int len;
}Wifi_Struct;

void Wifi_Init(int baud_rate);

void Wifi_Parse_Json(char *json);

#if defined ( __CC_ARM   )
#pragma anon_unions
#endif

#define ESP8266_USART(fmt, ...)  USART_printf (USART2, fmt, ##__VA_ARGS__)
#define PC_USART(fmt, ...)       printf(fmt, ##__VA_ARGS__)

#define RX_BUF_MAX_LEN 1024
extern struct STRUCT_USART_Fram {
    u8 Data_RX_BUF[RX_BUF_MAX_LEN];
    union {
        __IO u16 InfAll;
        struct {
            __IO u16 FramLength : 15;                               // 14:0
            __IO u16 FramFinishFlag : 1;                                // 15
        }InfBit;
    };

    union {
        __IO u16 State;
       struct {
            __IO u16 Connect_flag : 1;
            __IO u16 Get_Ip_flag : 1;
            __IO u16 ESP8266_Ready_flag : 1;
            __IO u16 Trans_Mode : 1;
            __IO u16 Send_OK_flag : 1;
            __IO u16 Sta_Mode : 1;
            __IO u16 res : 10;
        }StateFlagBit;
    };
}ESP8266_Fram_Record_Struct;

// 初始化和TCP功能函数
signed char ESP8266_start_TCP(void);
void Wifi_Init(int baud_rate);
void ESP8266_AT(void);
void ESP8266_Send_AT_Cmd(char *cmd, u32 time);
void USART_printf(USART_TypeDef *USARTx, char *Data, ...);

#endif
